<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 2018/5/21
 * Time: 18:00
 */

namespace app\admin\controller;
use app\api\model\Banner as BannerModel;
use app\api\validate\BannerValidate;

class Banner extends Common
{
    /**
     * banner列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index() {
        if (request()->isAjax()) {
            $data = BannerModel::all()->toArray();
            return admin_return_data('Banner列表', count($data), $data);
        }
        return view();
    }

    /**
     * 创建或者编辑品牌
     * @return \think\response\Json|\think\response\View
     * @throws \app\api\exception\ParameterException
     * @throws \think\exception\DbException
     */
    public function save_banner() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {
            $banner_validate = new BannerValidate();
            $banner_validate->goCheck();
            $param = $banner_validate->getDataByRule(input('param.'));
            $data = BannerModel::createOrUpdateBanner($param, $id);
            return json($data);
        } else {
            $data = [];
            if ($id > 0) {
                $data = BannerModel::get($id);
            }
            $this->assign('data', $data);

            return view();
        }
    }
}